<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 图标区域 -->
      <div class="avatar-box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 账号密码区域 -->
      <div class="account-box">
        <!-- 用户登录账号表单 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
          label-width="70px"
        >
          <!-- 用户名 -->
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="submitLoginForm">登陆</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 登陆表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      // 登陆表单校验规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在3到5个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到15个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    // 重置用户米密码
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 登陆
    submitLoginForm() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登陆失败')
        this.$message.success('登陆成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: rgb(106, 201, 238);
  height: 100%;
}

.login-box {
  position: absolute;
  width: 450px;
  height: 300px;
  border-radius: 10px;
  box-shadow: 2px 2px 2px 2px gainsboro;
  background-color: #fff;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.avatar-box {
  position: absolute;
  width: 130px;
  height: 130px;
  padding: 10px;
  border: 1px solid black;
  box-shadow: 0 0 10px 0 rgb(134, 134, 145);
  background-color: rgb(149, 198, 233);
  border-radius: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff;
  }
}

.account-box {
  width: 100%;
  padding: 90px 50px 50px 50px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
